package com.study.cloud.gateway.client.support;

import com.study.cloud.gateway.client.core.ApiProperties;
import com.study.cloud.gateway.common.rule.ServiceDefinition;
import com.study.cloud.gateway.common.rule.ServiceInstance;
import com.study.cloud.registry.api.RegistryCenter;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ServiceLoader;

/**
 * @author wangwancheng
 * @create 2025/7/19 19:39
 */
@Slf4j
public abstract class AbstractClientRegisterManager {

    @Getter
    private ApiProperties apiProperties;

    private RegistryCenter registryCenter;

    protected AbstractClientRegisterManager(ApiProperties apiProperties){
        this.apiProperties = apiProperties;

        //利用java的spi机制初始化注册中心
        registryCenter = ServiceLoader.load(RegistryCenter.class).iterator().next();
        //初始化registryCenter
        registryCenter.init(apiProperties.getRegisterAddress(), apiProperties.getEnv());
    }

    protected void register(ServiceDefinition serviceDefinition, ServiceInstance serviceInstance){
        registryCenter.register(serviceDefinition , serviceInstance);
    }
}
